package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created by admin on 2017/2/28.
 */

public class PasteButton extends Button {
    private static final String TAG = "PasteButton";

    Paint mPaint = new Paint();
    int padding = 5;
    float d = 4f;

    boolean isStart;
    boolean isFinishedForP5;

    public PasteButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackground(null);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (widthP1 == -1 && heightP4 == -1 && widthP5 == -1) {
            widthP1 = getWidth() - padding * 5;
            heightP4 = getHeight() - padding;
            widthP5 = getWidth() / 2 - padding;
        }
        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setColor(Color.argb(255, 65, 72, 75));
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);
        {
            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            Path path = new Path();
            path.moveTo(getWidth() - padding * 5, padding);

            if (isStart) {
                if (!drawP1(path)) ;
                else if (!drawP2(path)) ;
                else if (!drawP3(path)) ;
                else if (!drawP4(path)) ;
                else {
                    isFinishedForP5 = true;
                }
                invalidate();
            } else {

                path.lineTo(padding, padding);
                path.lineTo(padding, getHeight() - padding);
                path.lineTo(getWidth() - padding, getHeight() - padding);
                path.lineTo(getWidth() - padding, padding * 7);
            }


            canvas.drawPath(path, mPaint);

            canvas.restoreToCount(layerId);
        }

        {

            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

            if (isStart && isFinishedForP5) {
                widthP5 += d / 2;
                if (widthP5 >= getWidth()) {
                    isFinishedForP5 = false;
                    isStart = false;
                    widthP1 = -1;
                    heightP4 = -1;
                    widthP5 = -1;
                    heightP2 = padding;
                    widthP3 = padding;
                } else {
                    canvas.clipRect(getWidth() / 2 - padding - 2.5f, 0, widthP5, getHeight());
                }
            }
            if (!isStart || (isFinishedForP5 && isStart)) {

                Path path = new Path();
                path.moveTo(getWidth() / 2 - padding, getHeight() / 2 + padding);
                path.cubicTo(getWidth() / 2 - padding / 2f, padding * 3, getWidth() / 2, padding * 3, getWidth() - padding, padding * 3);
                path.lineTo(getWidth() - padding * 3, padding);
                path.moveTo(getWidth() - padding * 3, padding * 5);
                path.lineTo(getWidth() - padding, padding * 3);
                canvas.drawPath(path, mPaint);
            }

            canvas.restoreToCount(layerId);
        }

    }

    public void click() {
        if (!isStart) {
            isStart = true;
            invalidate();
        }
    }

    float widthP1 = -1;

    private boolean drawP1(Path path) {
        path.lineTo(widthP1, padding);
        if (widthP1 <= padding) {
            return true;
        }
        widthP1 -= d;
        if (widthP1 <= padding) {
            widthP1 = padding;
        }
        return false;
    }

    float heightP2 = padding;

    private boolean drawP2(Path path) {
        path.lineTo(padding, heightP2);
        if (heightP2 >= getHeight() - padding) {
            return true;
        }
        heightP2 += d;
        if (heightP2 >= getHeight() - padding) {
            heightP2 = getHeight() - padding;
        }
        return false;
    }

    float widthP3 = padding;

    private boolean drawP3(Path path) {
        path.lineTo(widthP3, getHeight() - padding);
        if (widthP3 >= getWidth() - padding) {
            return true;
        }
        widthP3 += d;
        if (widthP3 >= getWidth() - padding) {
            widthP3 = getWidth() - padding;
        }
        return false;
    }

    float heightP4 = -1;

    private boolean drawP4(Path path) {
        path.lineTo(getWidth() - padding, heightP4);
        if (heightP4 <= padding * 7) {
            return true;
        }
        heightP4 -= d;
        if (heightP4 <= padding * 7) {
            heightP4 = padding * 7;
        }
        return false;
    }

    float widthP5 = -1;
}
